import 'dart:math';

import 'package:flutter/material.dart';

import 'package:rxdart/rxdart.dart';

class RxDartDemo extends StatefulWidget {
  @override
  _RxDartDemoState createState() {
    return _RxDartDemoState();
  }
}

class _RxDartDemoState extends State<RxDartDemo> {
  var _changeData;
  final subject = PublishSubject<int>();
  @override
  void initState() {
    super.initState();
    subject.listen((data) => print('数据: $data'));
    subject.listen((data) => print('数据: $data'));
    Stream.fromIterable([300, 240, 400])
        .throttleTime(Duration(seconds: 1))
        .debounceTime(Duration(seconds: 1))
        .listen(print); // prints 1
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Wrap(
      children: [
        Text("数据变化之后显示到界面上 ${_changeData}"),
        Divider(),
        ActionChip(label: Text("subject add Data"),onPressed: addData,)

      ],
    );
  }

  void addData() {
    subject.add(Random().nextInt(100));
    subject.close();
  }

}
